package com.example.day03homework.Utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/*jdbc连接池*/
public class JDBCUtils {
    //封装一下数据库相关的信息
    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/newsdb";
    private static String userName = "root";
    private static String passWord = "Zhang1221?";

    //静态加载驱动
    static {
        try {
            Class.forName(driver);  //利用反射加载驱动
        } catch (ClassCastException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("驱动已加载");
    }

//    建立连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, userName, passWord);
    }

//    关闭资源
    public static void close(ResultSet rs, PreparedStatement pst, Connection con) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (pst != null) {
            pst.close();
        }
        if (con != null) {
            con.close();
        }
    }


}
